package com.example.technology.lovedemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.example.technology.lovedemo.like.TCHeartLayout;
import com.example.technology.lovedemo.like.TCHeartView;

public class MainActivity extends AppCompatActivity {
    private GestureDetector myGestureDetector;
    private Love ll_love;

    private TCHeartLayout heart_layout;
    private TCHeartLayout tc_heart_layout;
    private TCHeartView heart_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ll_love = (Love) findViewById(R.id.lovelayout);
        ImageView iamge = findViewById(R.id.iamge);
        //实例化GestureDetector
        myGestureDetector = new GestureDetector(this, new myOnGestureListener());
        //增加监听事件

        iamge.setOnTouchListener(new View.OnTouchListener() {

            @Override//可以捕获触摸屏幕发生的Event事件
            public boolean onTouch(View v, MotionEvent event) {
                //使用GestureDetector转发MotionEvent对象给OnGestureListener
                myGestureDetector.onTouchEvent(event);
                return true;
            }
        });

        //腾讯飘心
        heart_layout = findViewById(R.id.heart_layout);
        heart_view = findViewById(R.id.heart_view);
        heart_view.setImageResource(R.mipmap.zan1);
        heart_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                heart_layout.addFavor(1);
                heart_layout.addFavor();
            }
        });

        tc_heart_layout = findViewById(R.id.tc_heart_layout);
        heart_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                tc_heart_layout.getX()
                //        这里要记住为什么用view.post
//        View.post(Runnable)方法。在post(Runnable action)方法里，View获得当前线程（即UI线程）的Handler，
//        然后将action对象post到Handler里。在Handler里，它将传递过来的action对象包装成一个Message（Message的callback为action），
//        然后将其投入UI线程的消息循环中。在Handler再次处理该Message时，有一条分支（未解释的那条）就是为它所设，
//        直接调用runnable的run方法。而此时，已经路由到UI线程里，因此，我们可以毫无顾虑的来更新UI。 //感觉不是很有用
                //https://www.jb51.net/article/148761.htm  获取控件宽高
                Log.d("tgw5", "onTouchEvent: mWidth:" + tc_heart_layout.getWidth() + "--mHeight:" + tc_heart_layout.getHeight() + "--event.getX():" + heart_view.getX() + "--event.getY():" + heart_view.getY() + "--event.getRawX():" + tc_heart_layout.getX() + "--event.getY():" + tc_heart_layout.getY());
                tc_heart_layout.post(new Runnable() {
                    @Override
                    public void run() {
                        tc_heart_layout.addFavor(tc_heart_layout.getWidth(), tc_heart_layout.getHeight(), 4);
                    }
                });
            }
        });

    }

    class myOnGestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            ll_love.addLoveView(e.getRawX(), e.getRawY());
            return super.onDoubleTap(e);
        }
    }


    public void jumpSecond(View view){
        Intent intent = new Intent(MainActivity.this,SecondActivity.class);
        startActivity(intent);
    }

}

